package com.qf.ems.controller;

import com.github.pagehelper.PageInfo;
import com.qf.ems.entity.Emp;
import com.qf.ems.entity.Page;
import com.qf.ems.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
/*2021/10/22*/public class DMLController {
    @Autowired
    EmpService empService;
    @RequestMapping("/add")
    public String addEmp(@RequestParam(value = "empId",defaultValue = "0") Integer id,String name,Double salary,Integer age){
        //调用业务层进行数据的添加

        Emp emp=new Emp(id,name,salary, age);
        if(id>0){
            Integer result = empService.addEmp(emp);
        }
        Integer integer = empService.updateEmpInfo(emp);
        return "forward:page";
    }

    @RequestMapping("/delete")
    public String deleteEmp(Integer empId){
        //调用service进行数据的删除
        Integer result = empService.deleteOneInfo(empId);
        return "forward:page";
    }

    @RequestMapping("/page")
    public String selectPage(Model model, @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum){
        Page page =new Page(pageNum);
        List<Emp> emps = empService.selectAllEmp(page);
        PageInfo<Emp> pageInfo=new PageInfo<>(emps);
        model.addAttribute("pageInfo", pageInfo);
        return "emplist";
    }

    @RequestMapping("/SelectOne")
    public String selectOneById(Integer empId,Model model){
        //调用service进行数据查询
        Emp emp = empService.selectOneEmp(empId);
        model.addAttribute("emp", emp);
        return "addEmp";
    }
}
